package com.hhss.qishi.controller.sys;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.hhss.qishi.config.constant.ModuleConstants;
import com.hhss.qishi.config.constant.OperateConstants;
import com.hhss.qishi.config.log.webLog.WebLog;
import com.hhss.qishi.config.pagehelper.PageVO;
import com.hhss.qishi.controller.CommonController;
import com.hhss.qishi.entity.output.TreeNode;
import com.hhss.qishi.entity.system.Menu;
import com.hhss.qishi.service.system.MenuService;
import com.hhss.qishi.utils.ConvertObjectUtil;
import com.hhss.qishi.utils.ConvertPageUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * 菜单控制器
 * @Author :Administrator
 * @Description
 * @Date :2019-05-07 16:32.
 */
@RestController
@RequestMapping("menus")
public class MenuController extends CommonController<Menu> {

	@Autowired
	private MenuService menuService;

	@RequestMapping(value = "/", method = RequestMethod.GET)
	public Map<String, Object> getList(Menu menu) {
		Page<Menu> page = PageHelper.startPage(menu.getPage(), menu.getLimit());
		menuService.getList(menu);
		PageVO<Menu> vo = PageVO.build(page);
		return ConvertPageUtil.Convert(vo);
	}

	@RequestMapping(value = "/", method = RequestMethod.POST)
	@WebLog(module = ModuleConstants.MODULE_MENU, operate = OperateConstants.OPR_ADD)
	public int add(Menu menu) {
		return menuService.add(menu);
	}

	@RequestMapping(value = "/treeNode", method = RequestMethod.POST)
	public int addTreeNode(TreeNode treeNode) {
		Menu menu = new Menu();
		menu.setPid(treeNode.getPid());
		menu.setUuid(treeNode.getUuid());
		menu.setMenuName(treeNode.getName());
		return menuService.add(menu);
	}


	@RequestMapping(value = "/", method = RequestMethod.PUT)
	@WebLog(module = ModuleConstants.MODULE_MENU, operate = OperateConstants.OPR_UPDATE)
	public int update(Menu menu) {
		return menuService.updateById(menu);
	}

	@RequestMapping(value = "/treeNode", method = RequestMethod.PUT)
	public int update(TreeNode treeNode) {
		Menu menu = new Menu();
		menu.setPid(treeNode.getPid());
		menu.setUuid(treeNode.getUuid());
		menu.setMenuName(treeNode.getName());
		return menuService.updateByUuid(menu);
	}

	@RequestMapping(value = "/{uuid}", method = RequestMethod.DELETE)
	@WebLog(module = ModuleConstants.MODULE_MENU, operate = OperateConstants.OPR_DELETE)
	public int deleteById(@PathVariable String uuid) {
		// 级联删除子节点
		menuService.hasChildIfyDelete(uuid);
		return menuService.deleteByUuid(uuid);
	}

	/**
	 * 获取菜单树
	 * @param menu
	 * @return
	 */
	@RequestMapping(value = "/menuTree", method = RequestMethod.GET)
	public String getMenuOpts(Menu menu) {
		List<Menu> menus = menuService.getMenuTreeAsync(menu);
		List<TreeNode> treeNodes = new ArrayList<>();
		treeNodes.add(getRootNote());
		for(Menu m : menus){
			TreeNode node = new TreeNode();
			node.setUuid(m.getUuid());
			node.setPid(m.getPid());
			node.setName(m.getMenuName());
			treeNodes.add(node);
		}
		return ConvertObjectUtil.toJson(treeNodes);
	}

	/**
	 * ztree根节点
	 * @return
	 */
	private TreeNode getRootNote(){
		TreeNode node = new TreeNode();
		node.setUuid("0");
		node.setPid("-1");
		node.setType("root");
		node.setName("菜单树");
		return node;
	}
}
